#include "animatedbutton.h"

void AnimatedButton::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // // 应用缩放变换（以按钮中心为基准）
    // QTransform transform;
    // transform.translate(width()/2, height()/2);
    // transform.scale(m_scaleFactor, m_scaleFactor);
    // transform.translate(-width()/2, -height()/2);
    // painter.setTransform(transform);

    // 绘制渐变背景
    QLinearGradient gradient(rect().topLeft(), rect().bottomRight());
    gradient.setColorAt(0, m_color.lighter(120));
    gradient.setColorAt(1, m_color.darker(120));
    painter.setBrush(gradient);

    // 绘制圆角矩形
    painter.setPen(Qt::NoPen);
    painter.drawRoundedRect(rect(), 8, 8);

    // // 绘制文字
    // painter.setFont(font());
    // painter.setPen(palette().buttonText().color());
    // painter.drawText(rect(), Qt::AlignCenter, text());

    // QPushButton::paintEvent(event);
}

void AnimatedButton::setupAnimations() {
    // // 缩放动画
    // QPropertyAnimation *scaleAnim = new QPropertyAnimation(this, "scaleFactor");
    // scaleAnim->setDuration(300);
    // scaleAnim->setEasingCurve(QEasingCurve::OutBack);
    // scaleAnim->setStartValue(1.0);
    // scaleAnim->setEndValue(0.95);

    // 颜色渐变动画
    QPropertyAnimation *colorAnim = new QPropertyAnimation(this, "buttonColor");
    colorAnim->setDuration(300);
    colorAnim->setStartValue(QColor("#4CAF50"));
    colorAnim->setEndValue(QColor("#FFC107"));

    // 组合动画
    animationGroup = new QParallelAnimationGroup(this);
    // animationGroup->addAnimation(scaleAnim);
    animationGroup->addAnimation(colorAnim);

    // 连接点击信号和恢复动画
    connect(this, &QPushButton::clicked, this, &AnimatedButton::startAnimation);
    connect(animationGroup, &QParallelAnimationGroup::finished, [this](){
        if(animationGroup->direction() == QAbstractAnimation::Forward) {
            animationGroup->setDirection(QAbstractAnimation::Backward);
            animationGroup->start();
        }
    });
}
